<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class linksController extends Controller
{
    //友情连接首页
    public function index(Request $request)
    {
    	$db = DB::table('storeLinks');
    	
    	//保存搜索的条件
    	$where = [];

    	if ( $request -> has('linksName') ){

    		$db->where('linksName','like',"%{$request->input('linksName')}%");
    			$where['linksName'] = $request->input('linksName');

    	}
    	$list = $db->paginate(6);

    	
    	return view('admin.storelinks.links',['list' => $list,'where' => $where ]);
    }

    //添加友情连接页面
    public function create()
    {
    	return view('admin.storelinks.create');
    }

    //执行添加页面
    public function store(Request $request)
    {
    	//自定义错误信息
    	$message = [
    		'required' => '信息不能为空',

    	];

    	//设定验证规则
    	$this -> validate($request,[
    		'linksName' => 'required',
    		'linksUrl' => 'required',
    		],$message);

    	//添加数据
    	$data = $request -> except('_token');

    	$id = DB::table('storeLinks')->insertGetId($data);

    	if ( $id > 0 ) {

    		return redirect('/admin/storeLinks')->with('msg','添加数据成功'); 
    	}
    }

    //删除连接
    public function destroy($id, Request $request)
    {
    	$row = DB::table('storeLinks')->where('linksId',$id)->delete();

    	if ( $row > 0 ) {

    		return back() -> with('msg','删除成功');
    	}
    }

    //修改页面
    public function edit($id)
    {
    	//获取修改数据
    	$list = DB::table('storeLinks')->where('linksId',$id)->first();

    	return view('admin.storelinks.edit',['list' => $list]);
    }

    //执行修改
   public function update($id,Request $request)
   {

   	//自定义错误信息
   	$message = [
   		'required' => '信息不能为空',
   	];

   	//设置验证规则
   	$this->validate($request,[
   			'linksName' => 'required',
   			'linksUrl' => 'required',

   		],$message);

   	//获取数据
   	$data = $request->except('_token','_method');
   	//执行修改
   	$row = DB::table('storeLinks')->where('linksId',$id)->update($data);

   	return redirect('/admin/storeLinks')->with('msg','修改成功');
   }

}
